package com.swing.sky.web.generator.util;

import com.swing.sky.web.generator.Gen;
import com.swing.sky.web.generator.config.GenConfig;
import com.swing.sky.web.generator.config.MybatisConfig;
import com.swing.sky.web.generator.constant.ModuleConstants;
import com.swing.sky.web.generator.dao.GenDAO;
import com.swing.sky.web.generator.domain.Column;
import com.swing.sky.web.generator.domain.Module;
import com.swing.sky.web.generator.domain.ModuleHouse;
import com.swing.sky.web.generator.domain.Table;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author swing
 */
public class ModuleHouseInitializer {
    private static final Logger log = LoggerFactory.getLogger(Gen.class);
    public static GenDAO genDAO;

    public static void init() {
        try {
            genDAO = MybatisConfig.getDAO();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取本次生成的所有ModuleHouse集合
     */
    public static List<ModuleHouse> buildModuleHouses() {
        init();
        List<ModuleHouse> moduleHouses = new ArrayList<>();
        //数据库名
        String schemaName = PropertyUtils.readSky("schemaName");
        for (int i = 1; i < 100; i++) {
            String tableName = PropertyUtils.readSky("table_" + i);
            if (tableName == null) {
                break;
            }
            ModuleHouse moduleHouse = new ModuleHouse();
            //获取该moduleHouse中的所有module
            List<Module> modules = null;
            if ("off".equalsIgnoreCase(PropertyUtils.readSky("table_" + i + ".basic.enable"))) {
                modules = basicOffModuleFilter();
            } else {
                modules = basicOnModuleFilter();
            }
            MapUtils.setFileNameByTableName(tableName, modules);

            Table table1 = new Table();
            table1.setTableSchema(schemaName);
            table1.setTableName(tableName);
            Table table = genDAO.listTables(table1).get(0);
            if (table == null) {
                log.warn("名为 {} 的表不存在，请检查配置", tableName);
            }

            //获取该表的的所有列对象(并包装为GenColumn)
            Column column = new Column();
            column.setTableSchema(schemaName);
            column.setTableName(tableName);
            List<Column> columns = genDAO.listColumns(column);

            moduleHouse.setTable(table);
            moduleHouse.setModules(modules);
            moduleHouse.setColumns(MapUtils.getGenColumnsByColumns(columns));
            moduleHouses.add(moduleHouse);
        }
        //关联表的处理
        for (int i = 1; i < 100; i++) {
            String s = PropertyUtils.readSky("link_table_" + i);
            if (s == null) {
                break;
            }
            String[] split = s.split(",");
            String leftHouseName = split[0];
            String tableName = split[1];
            String rightHouseName = split[2];
            ModuleHouse moduleHouse = new ModuleHouse();
            moduleHouse.setLeftHouseName(leftHouseName);
            moduleHouse.setRightHouseName(rightHouseName);
            List<Module> modules = linkTableModuleFilter();
            MapUtils.setFileNameByTableName(tableName, modules);
            Table table1 = new Table();
            table1.setTableSchema(schemaName);
            table1.setTableName(tableName);
            Table table = genDAO.listTables(table1).get(0);
            if (table == null) {
                log.warn("名为 {} 的表不存在，请检查配置", tableName);
            }
            moduleHouse.setTable(table);
            moduleHouse.setModules(modules);
            //获取该表的的所有列对象(并包装为GenColumn)
            Column column = new Column();
            column.setTableSchema(moduleHouse.getTable().getTableSchema());
            column.setTableName(moduleHouse.getTable().getTableName());
            List<Column> columns = genDAO.listColumns(column);
            moduleHouse.setColumns(MapUtils.getGenColumnsByColumns(columns));
            moduleHouses.add(moduleHouse);
        }
        return moduleHouses;
    }

    /**
     * 当table.basic.enable=on时需要的模块
     */
    public static List<Module> basicOnModuleFilter() {
        List<String> moduleNames = new ArrayList<>();
        List<Module> modules = new ArrayList<>();
        moduleNames.add(ModuleConstants.BASIC_DOMAIN);
        moduleNames.add(ModuleConstants.DOMAIN_BASIC);
        moduleNames.add(ModuleConstants.BASIC_DAO);
        moduleNames.add(ModuleConstants.DAO_BASIC);
        moduleNames.add(ModuleConstants.BASIC_MAPPER);
        moduleNames.add(ModuleConstants.MAPPER_BASIC);
        moduleNames.add(ModuleConstants.BASIC_SERVICE);
        moduleNames.add(ModuleConstants.SERVICE_BASIC);
        moduleNames.add(ModuleConstants.SERVICE_IMPL);
        for (String moduleName : moduleNames) {
            modules.add(GenConfig.getModule(moduleName));
        }
        return modules;
    }

    /**
     * 当table.basic.enable=off时需要的模块
     */
    public static List<Module> basicOffModuleFilter() {
        List<String> moduleNames = new ArrayList<>();
        List<Module> modules = new ArrayList<>();
        moduleNames.add(ModuleConstants.DOMAIN);
        moduleNames.add(ModuleConstants.BASIC_DAO);
        moduleNames.add(ModuleConstants.DAO_BASIC);
        moduleNames.add(ModuleConstants.MAPPER);
        moduleNames.add(ModuleConstants.BASIC_SERVICE);
        moduleNames.add(ModuleConstants.SERVICE_BASIC);
        moduleNames.add(ModuleConstants.SERVICE_IMPL);
        for (String moduleName : moduleNames) {
            modules.add(GenConfig.getModule(moduleName));
        }
        return modules;
    }

    /**
     * 当table.basic.enable=off时需要的模块
     */
    public static List<Module> linkTableModuleFilter() {
        List<String> moduleNames = new ArrayList<>();
        List<Module> modules = new ArrayList<>();
        moduleNames.add(ModuleConstants.DOMAIN);
        moduleNames.add(ModuleConstants.BASIC_LINK_DAO);
        moduleNames.add(ModuleConstants.DAO_LINK);
        moduleNames.add(ModuleConstants.LINK_MAPPER);
        for (String moduleName : moduleNames) {
            modules.add(GenConfig.getModule(moduleName));
        }
        return modules;
    }


}
